package cn.pn.project.service;

import cn.pn.project.common.RedisUtils;
import cn.pn.project.mapper.schedule.CommoditysessionDao;
import cn.pn.project.pojo.schedule.Commoditysession;
import com.alibaba.fastjson.JSON;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;
import java.util.List;

@RestController
public class CommoditySessionService {

    @Autowired
    private CommoditysessionDao commoditysessionDao;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 根据场次时间id查询场地id
     * @param sessionIds 场次id，是多个，用逗号隔开
     * @return
     * @throws SQLException
     */
    @RequestMapping(value = "getCommoditysessionBySessionId",method = RequestMethod.POST)
    public Object getCommoditysessionBySessionId(@RequestParam("sessionIds") String sessionIds) throws SQLException{
        System.out.println("============="+sessionIds);
        System.out.println(JSON.toJSONString(commoditysessionDao.getCommoditysessionBySessionId(sessionIds),true));
        String key="sessionIdByPlaceId"+sessionIds;
        if(redisUtils.exist(key)){
            System.out.println("从redis中获取数据");
            return redisUtils.get(key);
        }else {
            if (redisUtils.set(key, commoditysessionDao.getCommoditysessionBySessionId(sessionIds))) {
                System.out.println("把根据场次时间id查询场地id放入到redis中");
            }
            return commoditysessionDao.getCommoditysessionBySessionId(sessionIds);
        }

    }

    /**
     * 根据场地id查询场次信息
     * @param placeId
     * @return
     * @throws SQLException
     */
    @RequestMapping(value = "/getCommoditysessionByPlaceId",method = RequestMethod.POST)
    public Object getCommoditysessionByPlaceId(@RequestParam("placeId") Integer placeId) throws SQLException{
        String key="PlaceIdBySession"+placeId;
        if(redisUtils.exist(key)){
            System.out.println("从redis中获取数据");
            return redisUtils.get(key);
        }else {
            if(redisUtils.set(key,commoditysessionDao.getCommoditysessionByPlaceId(placeId))){
                System.out.println("将根据场地id查询场次信息方法redis中");
            }
            return commoditysessionDao.getCommoditysessionByPlaceId(placeId);
        }

    }

    /**
     * 根据场地id和场次时间查询场次id
     * @param placeId
     * @param sessionTime
     * @return
     * @throws SQLException
     */
    @RequestMapping(value = "getCommoditysessionByPlaceIdAndSessionTime",method = RequestMethod.POST)
    public Commoditysession getCommoditysessionByPlaceIdAndSessionTime(@RequestParam("placeId")Integer placeId,@RequestParam("sessionTime")String sessionTime ) throws SQLException{
        String key="placeId"+placeId+"sessionTime"+sessionTime;
        if(redisUtils.exist(key)){
            System.out.println("根据场地id和场次时间查询场次id从redis中获取数据");
            return (Commoditysession)redisUtils.get(key);
        }else {
            if (redisUtils.set(key, commoditysessionDao.getCommoditysessionByPlaceIdAndSessionTime(placeId, sessionTime))) {
                System.out.println("将数据保存在redis中");
            }
            return commoditysessionDao.getCommoditysessionByPlaceIdAndSessionTime(placeId, sessionTime);
        }
    }
}
